import 'package:ble_measure_plugin/devices/base_device_model.dart';
import 'package:ble_measure_plugin/model/blood_oxygen_model.dart';

import '../ble_measure_plugin.dart';

class LepuOxygenDeviceModel extends BaseDeviceMeasureModel {
  @override
  String get serviceUuid => "6e400001-b5a3-f393-e0a9-e50e24dcca9e";

  @override
  String get characteristicReadable => "6e400003-b5a3-f393-e0a9-e50e24dcca9e";

  @override
  String get characteristicWriteable => "6e400002-b5a3-f393-e0a9-e50e24dcca9e";

  EventHandlerMap<BloodOxygenModel>? _oxygenResultHandler;

  void addListener({
    EventHandlerMap<BloodOxygenModel>? resultListener,
  }) {
    _oxygenResultHandler = resultListener;
  }

  var model = BloodOxygenModel();
  var hasSendResult = false;

  @override
  void onReceiveData(List<String> data) {
    var type = data[4];
    switch (type) {
      ///工作测量数据
      case "21":
      // case "01":
        model.status = "0";
        // if (type == "01") {
        //   model.oxygen = int.parse(data[5], radix: 16).toString();
        // }
        if (type == "21") {
          var mode = data[5];
          if (mode == "01") {
            var step = data[6];
            if (step == "05") {
              model.status = "1";
            } else {
              model.status = "0";
              if(step=="02"){
                model.remainTime = int.parse(data[7],radix: 16).toString();
              }
              if(step=="03"){
                model.oxygen = int.parse(data[7],radix: 16).toString();
                model.plusRate = int.parse(data[8],radix: 16).toString();
                print(data);
                model.status = "1";
              }
              if(step=="04"){
                hasSendResult = true;
                model.status = "1";
                if(data[7]=="FF"){
                  model.oxygen = "";
                  model.plusRate = "";
                }
                model.resultStr = pareseResult(data[7]);
                model.resultCode = data[7];
              }
            }

          }
        }
        // print(model.toString());
        if(_oxygenResultHandler!=null){
          _oxygenResultHandler!(model);
        }
        break;
    }
  }

  @override
  void init() {
    super.init();
    model = BloodOxygenModel();
  }

  @override
  void onOpenSuccess() {
    super.onOpenSuccess();
    // model = BloodOxygenModel();
  }

  String pareseResult(String code){
    var result = "";
    switch(code){
      case "00":
        result = "脉搏节律未见异常";
        break;
      case "01":
        result = "疑似脉率稍快";
        break;
      case "02":
        result = "疑似脉率过快";
        break;
      case "03":
        result = "疑似阵发性脉率过快";
        break;
      case "04":
        result = "疑似脉率稍缓";
        break;
      case "05":
        result = "疑似脉率过缓";
        break;
      case "06":
        result = "疑似偶发脉搏间期缩短";
        break;
      case "07":
        result = "疑似脉搏间期不规则";
        break;
      case "08":
        result = "疑似脉率稍快伴有偶发脉搏间期缩短";
        break;
      case "09":
        result = "疑似脉率稍缓伴有偶发脉搏间期缩短";
        break;
      case "0A":
        result = "疑似脉率稍缓伴有脉搏间期不规则";
        break;
      case "FF":
        result = "信号质量差请重新测量";
        break;
    }
    return result;
  }
}
